<template>
  <template v-if="getShow">
    <LoginFormTitle class="enter-x" />
    <Form
      layout="horizontal"
      class="p-4 enter-x"
      :model="formData"
      :rules="getFormRules"
      ref="formRef"
    >
      <FormItem :label="t('sys.login.userName')" name="userName" class="enter-x">
        <Input
          class="fix-auto-fill"
          v-model:value="formData.userName"
          :placeholder="t('sys.login.userName')"
        />
      </FormItem>
      <FormItem :label="t('sys.login.phoneNumber')" name="phoneNumber" class="enter-x">
        <Input
          v-model:value="formData.phoneNumber"
          :placeholder="t('sys.login.phoneNumber')"
          :maxlength="11"
          class="fix-auto-fill"
        />
      </FormItem>
      <FormItem :label="t('sys.login.email')" name="email" class="enter-x">
        <Input
          v-model:value="formData.email"
          :placeholder="t('sys.login.email')"
          class="fix-auto-fill"
        />
      </FormItem>
      <FormItem :label="t('sys.login.height')" name="height" class="enter-x">
        <InputNumber
          v-model:value="formData.height"
          :min="0.5"
          step="0.5"
          :placeholder="t('sys.login.height')"
        />
      </FormItem>
      <FormItem :label="t('sys.login.age')" name="age" class="enter-x">
        <InputNumber
          v-model:value="formData.age"
          :min="0.5"
          step="0.5"
          :placeholder="t('sys.login.age')"
        />
      </FormItem>
      <FormItem :label="t('sys.login.weight')" name="weight" class="enter-x">
        <InputNumber
          v-model:value="formData.weight"
          :min="0.5"
          step="0.5"
          :placeholder="t('sys.login.weight')"
        />
      </FormItem>
      <FormItem :label="t('sys.login.sex')" name="sex" class="enter-x">
        <RadioGroup v-model:value="formData.sex">
          <Radio :value="SexEnum.MALE">{{ t('sys.login.male') }}</Radio>
          <Radio :value="SexEnum.FEMALE">{{ t('sys.login.female') }}</Radio>
        </RadioGroup>
      </FormItem>
      <FormItem :label="t('sys.login.password')" name="password" class="enter-x">
        <StrengthMeter v-model:value="formData.password" :placeholder="t('sys.login.password')" />
      </FormItem>
      <FormItem :label="t('sys.login.confirmPassword')" name="confirmPassword" class="enter-x">
        <InputPassword
          visibilityToggle
          v-model:value="formData.confirmPassword"
          :placeholder="t('sys.login.confirmPassword')"
        />
      </FormItem>

      <FormItem class="enter-x" name="policy">
        <!-- No logic, you need to deal with it yourself -->
        <Checkbox v-model:checked="formData.policy" size="small">
          {{ t('sys.login.policy') }}
        </Checkbox>
      </FormItem>

      <Button
        type="primary"
        class="enter-x"
        size="large"
        block
        @click="handleRegister"
        :loading="loading"
      >
        {{ t('sys.login.registerButton') }}
      </Button>
      <Button size="large" block class="mt-4 enter-x" @click="handleBackLogin">
        {{ t('sys.login.backSignIn') }}
      </Button>
    </Form>
  </template>
</template>
<script lang="ts" setup>
  import { reactive, ref, unref, computed } from 'vue'
  import LoginFormTitle from './LoginFormTitle.vue'
  import { Form, Input, Button, Checkbox, Radio, RadioGroup, InputNumber } from 'ant-design-vue'
  import { StrengthMeter } from '/@/components/StrengthMeter'
  import { useI18n } from '../../../hooks/web/useI18n'
  import { useLoginState, useFormRules, useFormValid, LoginStateEnum } from './useLogin'
  import { SexEnum } from '/@/enums/sexEnum'
  import { useMessage } from '../../../hooks/web/useMessage'
  import { registerApi } from '/@/api/sys/user'
  import { registerParams } from '/@/api/sys/model/userModel'

  const { createMessage } = useMessage()

  const FormItem = Form.Item
  const InputPassword = Input.Password
  const { t } = useI18n()
  const { handleBackLogin, getLoginState } = useLoginState()

  const formRef = ref()
  const loading = ref(false)

  const formData: registerParams = reactive<registerParams>({
    userName: '',
    password: '',
    confirmPassword: '',
    phoneNumber: '',
    sex: SexEnum.MALE,
    height: '',
    age: '',
    weight: '',
    email: '',
    policy: false,
  })

  const { getFormRules } = useFormRules(formData)
  const { validForm } = useFormValid(formRef)

  const getShow = computed(() => unref(getLoginState) === LoginStateEnum.REGISTER)

  async function handleRegister() {
    const data = await validForm()
    if (!data) return
    console.log(data)
    const res = await registerApi(data)
    console.log(res)
    createMessage.success(t('sys.login.registerSuccess'))
  }
</script>
